package sk.vasicek.drinkmanager.service;

import sk.vasicek.drinkmanager.R;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class BackgroundAdapter extends BaseAdapter {
	int mGalleryItemBackground;
	private Context mContext;
	private static Style style;

	public enum Style {
		Dark(0), Steel(1), Titan(2);

		private Integer[] backArr = { R.drawable.back_dark, R.drawable.back_steel, R.drawable.back_titan };
		private Integer[] charArr = { R.drawable.street_boy_side, R.drawable.street_boy_side, R.drawable.street_boy_side };
		private Integer[] poiArr = { R.drawable.beer_4, R.drawable.beer_4, R.drawable.beer_4 };
		private Integer back;
		private Integer charIcon;
		private Integer poiIcon;

		private Style(int id) {
			this.back = backArr[id];
			this.charIcon = charArr[id];
			this.poiIcon = poiArr[id];
		}

		/**
		 * @return the back
		 */
		public Integer getBack(Context context) {
			SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

			int backgroundId = Integer.parseInt(prefs.getString("BackgroundPref", "0"));
			back = backArr[backgroundId];
			return back;
		}

		public Integer getBack() {
			return back;
		}

		/**
		 * @param back
		 *            the back to set
		 */
		public void setBack(Integer back) {
			this.back = back;
		}

		/**
		 * @return the charIcon
		 */
		public Integer getCharIcon(Context context) {
			SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

			int backgroundId = Integer.parseInt(prefs.getString("BackgroundPref", "0"));
			charIcon = charArr[backgroundId];
			return charIcon;
		}

		public Integer getCharIcon() {
			return charIcon;
		}

		/**
		 * @param charIcon
		 *            the charIcon to set
		 */
		public void setCharIcon(Integer charIcon) {
			this.charIcon = charIcon;
		}

		/**
		 * @return the poiIcon
		 */
		public Integer getPoiIcon(Context context) {
			SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

			int backgroundId = Integer.parseInt(prefs.getString("BackgroundPref", "0"));
			poiIcon = poiArr[backgroundId];
			return poiIcon;
		}

		public Integer getPoiIcon() {
			return poiIcon;
		}

		/**
		 * @param poiIcon
		 *            the poiIcon to set
		 */
		public void setPoiIcon(Integer poiIcon) {
			this.poiIcon = poiIcon;
		}

	}

	public BackgroundAdapter(Context c) {

		mContext = c;
		TypedArray attr = mContext.obtainStyledAttributes(R.styleable.BackgroundGalery);
		mGalleryItemBackground = attr.getResourceId(R.styleable.BackgroundGalery_android_galleryItemBackground, 0);
		attr.recycle();
	}

	public int getCount() {
		return Style.values().length;
	}

	public Object getItem(int position) {
		return Style.values()[position].getBack();
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView = new ImageView(mContext);

		imageView.setImageResource(Style.values()[position].getBack());
		imageView.setLayoutParams(new Gallery.LayoutParams(200, 125));
		imageView.setScaleType(ImageView.ScaleType.FIT_XY);
		imageView.setBackgroundResource(mGalleryItemBackground);

		return imageView;
	}

	public static Drawable updateBackgroundFromPrefs(SharedPreferences prefs, Activity context) {
		// background
		int backgroundId;
		RelativeLayout base;
		// background
		setStatusBarVisibilityForAndroidVersion(context);
		backgroundId = Integer.parseInt(prefs.getString("BackgroundPref", "0"));
		BackgroundAdapter backgroundAdapter = new BackgroundAdapter(context);
		base = (RelativeLayout) context.findViewById(R.id.base);

		Drawable background = context.getResources().getDrawable(Style.values()[backgroundId].getBack());
		base.setBackgroundDrawable(background);
		return background;
	}

	@TargetApi(11)
	public static void setStatusBarVisibilityForAndroidVersion(Activity context) {
		RelativeLayout statusbar = (RelativeLayout) context.findViewById(R.id.statusbar);

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			ActionBar actionBar = context.getActionBar();
			actionBar.setDisplayHomeAsUpEnabled(true);
			if (statusbar != null) {
				statusbar.getLayoutParams().height = 0;
				statusbar.setVisibility(View.INVISIBLE);
			}
		}

	}

	public void setStyle(Style style) {
		this.style = style;
	}

	public static Style getStyle(int styleId) {
		return Style.values()[styleId];
	}
}
